package 链表;

/**
 * @ClassName : Test
 * @Author : 骆发茂
 * @Date: 2021/12/9 18:39
 * @Description :
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(validBoard(new String[]{"XO ","XO ","XO "}));
    }
    public static boolean validBoard(String[] board) {
        int xCount = 0, oCount = 0;
        for (String row : board)
            for (char c : row.toCharArray()) {
                if (c == 'X') xCount++;
                if (c == 'O') oCount++;
            }
//X与O 一样多，或者X比O多一个(X赢则X多一个，O赢则一样多)
        if (oCount != xCount && oCount != xCount - 1) return false;
        if (win(board, "XXX") && oCount != xCount - 1) return false;
        if (win(board, "OOO") && oCount != xCount) return false;
        return true;
    }

    public static boolean win(String[] board, String flag) {
        for (int i = 0; i < 3; ++i) {
//纵向3连
            if (flag.equals("" + board[i].charAt(0) + board[i].charAt(1) +
                    board[i].charAt(2)))
                return true;
//横向3连
            if (flag.equals(board[i]))
                return true;
        } // \向3连
        if (flag.equals("" +
                board[0].charAt(0) + board[1].charAt(1) + board[2].charAt(2)))
            return true;
// /向3连
        if
        (flag.equals("" + board[0].charAt(2) + board[1].charAt(1) + board[2].charAt(0)))
            return true;
        return false;
    }
}